﻿using KinonekoSoftware.CodariaDB.Documents.Keywords;
using KinonekoSoftware.CodariaDB.IO.V4;
using KinonekoSoftware.Platform.Windows;
using KinonekoSoftware.Studio.Pages.Galleries.Abilities;

namespace KinonekoSoftware.Studio.Pages.Galleries
{
    partial class InstanceGalleryViewModel2
    {
        //-------------------------------------------------
        //
        //          ReadOnly Fields
        //
        //-------------------------------------------------

        private static readonly Dictionary<InstanceType, GalleryScraffolds> _mapByEnum   = new Dictionary<InstanceType, GalleryScraffolds>();
        private static readonly Dictionary<string, GalleryScraffolds>       _mapByString = new Dictionary<string, GalleryScraffolds>();



        private static GalleryScraffolds GetLegacyItem(InstanceCatalog catalog)
        {
            if (!_mapByEnum.TryGetValue(catalog.Type, out var vm))
            {
                vm = new ModernListGalleryViewModel
                {
                    Type = catalog.Type,
                };

                _mapByEnum.Add(catalog.Type, vm);
            }

            return vm;
        }

        private static GalleryScraffolds GetOrCache(InstanceType type, Func<GalleryScraffolds> factory)
        {

            if (_mapByEnum.TryGetValue(type, out var vm))
            {
                return vm;
            }

            vm = factory();
            _mapByEnum.Add(type, vm);

            //
            //
            return vm;
        }


        private static GalleryScraffolds GetGalleryItem(InstanceCatalog catalog)
        {
            if (catalog is null)
            {
                return null;
            }

            GalleryScraffolds vm;

            if (catalog.UseCustomType)
            {
                if (!_mapByString.TryGetValue(catalog.CustomType, out vm))
                {
                    vm = new ModernListGalleryViewModel
                    {
                        CustomType = catalog.CustomType,
                    };

                    _mapByString.Add(catalog.CustomType, vm);
                }
            }
            else
            {
                vm = (catalog.Type) switch
                {
                    InstanceType.Ability => GetOrCache(InstanceType.Ability, () => new AbilityGalleryViewModel { Type = InstanceType.Ability }),
                    _                    => GetLegacyItem(catalog),
                };
            }


            return vm;
        }
    }
}